@extends('WShop::layout')
@section('content')
    @component('WShop::public.header')
        @slot('page_title'){{$page_title}} @endslot
        @slot('btn')
            <a href="javascript:;" class="mui-pull-right mui-text-bai" style="margin-top: 12px;font-size: 12px;">规则 </a>
        @endslot
    @endcomponent
    <div class="mui-content mui-scroll-wrapper" id="pullrefresh">
        <div class="mui-scroll">

            <div class="mui-table-view mui-table-view-chevron mui-bottom mui-daishou">
                <div class="mui-daishou-list bg-fff">
                    <p class="mui-clearfix"><span class="mui-pull-right"><i class="fa fa"></i>购买日{{get_rule('dh_agent_sales_day')}}天之后结算</span></p>
                    <div class="flex">

                        <img src="{{asset(config('view.frontend.wap_login').'/images/yuanbao.png')}}" alt="" width="30">
                        <div class="daishou">
                            <div>理财期限：{{get_rule('dh_agent_sales_day')}}天 </div>
                            <div>预计收益：{{get_rule('dh_award2')}}{{get_currency()['money2']}}
                                {{get_rule('dh_award1')}}{{get_currency()['money1']}}</div>
                            <div> 复投金额：
                                {{get_rule('dh_agent_need_score')}}{{get_currency()['money2']}}+
                                {{get_rule('dh_agent_need_score')}}{{get_currency()['money1']}}

                            </div>
                            <div> 账户余额：{{$wallet->money2}}{{get_currency()['money2']}}</div>
                            <div> 账户余额：{{$wallet->money1}}{{get_currency()['money1']}}</div>
                        </div>

                    </div>
                    <div class="mui-text-right">
                        @if($wallet->money2-get_rule('dh_agent_need_score')>=0)
                            @if($is_open)
                                <a href="javascript:;" id="agent-sales" class="mui-btn mui-btn-blue">立即代售</a>
                            @else
                                <a href="javascript:;" disabled class="mui-btn mui-disabled">暂未开放</a>
                            @endif
                        @else
                            <a href="javascript:;" disabled class="mui-btn mui-disabled">余额不足</a>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
    @include('WShop::public.foot_bar',['active'=>'agent'])
@endsection
@push('scripts')
    <script>
        var btns = $('#agent-sales')
        for(var i=0;i<btns.length;i++){
            btns[i].addEventListener('tap',function(){
                mui.confirm('确定代售此产品？','提示',function(e){
                    if(e.index==1){
                        sendDhAgent();
                    }
                })
            })
        }
    </script>
    <script>
        function sendDhAgent()
        {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: "{{route('f_Wap_Agent_dhAgent')}}",
            }).done(function (response) {
                if(response.status){
                    mui.toast(response.msg);
                    setTimeout(function () {
                        location.href = response.url;
                    }, 2000);
                }else{
                    mui.toast(response.msg);
                }
            }).fail(function (XMLHttpRequest) {
                if (XMLHttpRequest.status == 422) {
                    var errors = XMLHttpRequest.responseJSON.errors;
                    if(typeof errors == 'object') {
                        for (var index in errors) { // 不推荐这样
                            mui.toast(errors[index][0]);
                            break;
                        }
                    }else{
                        mui.toast(XMLHttpRequest.responseJSON.msg);

                    }
                }else{
                    mui.toast('网络异常,请检查连接');
                }

            }).always(function () {

            });
        }
    </script>
@endpush